package org.manager.generic.web;

import java.lang.reflect.Field;

import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.springframework.web.servlet.view.json.writer.jsonlib.JsonlibJsonWriterConfiguratorTemplate;

public class MoyanJsonlibJsonWriterConfiguratorTemplate extends
		JsonlibJsonWriterConfiguratorTemplate {

	@Override
	public JsonConfig getJsonConfig() {
		JsonConfig config = new JsonConfig();

		// Exclude all date properties
		config.setJsonPropertyFilter(new PropertyFilter() {
			@SuppressWarnings("unchecked")
			public boolean apply(Object source, String name,Object value) {
				
				if(source instanceof java.util.Map){
					return false;
				}
				
				
				Field declaredField = null;
				try {
					declaredField = source.getClass().getDeclaredField(name);
				} catch (NoSuchFieldException e) {
					e.printStackTrace();
				}
				
				//干脆只能转String.呵呵，别怪我狠心啊。
				if(!declaredField.getType().isAssignableFrom(String.class)
						&& !declaredField.getType().isAssignableFrom(boolean.class)){
					return true;
				}
				
				/*// 忽略集合
				if (declaredField != null) {
					if (declaredField.getType().isAssignableFrom(Collection.class)
							|| declaredField.getType().isAssignableFrom(Set.class)
							|| declaredField.getType().isAssignableFrom(Map.class)
							|| declaredField.getType().isAssignableFrom(List.class) ){
						return true;
					}
				}*/
				return false;
			}
		});
		return config;
	}

}
